﻿using AutoMapper;
using Modules;
using Modules.Project;
using Pipelines.Sockets.Unofficial.Arenas;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Entity;
using Modules.System.User;
using Modules.Common;

namespace ManagementApi.Models.Project;

/// <summary>
/// 保证金退还Dto
/// </summary>
public class ProjectGuaranteeRefundDto : OperateDto
{
    /// <summary>
    ///
    ///</summary>
    public int Id { get; set; }

    /// <summary>
    /// 项目Id
    /// </summary>
    public int projectId { get; set; }

    /// <summary>
    /// 需要退还保证金的缴纳信息Id
    ///</summary>
    public int? GuaranteePaymentId { get; set; }

    /// <summary>
    /// 缴纳主体名称
    /// </summary>
    public string? PaymentName { get; set; }

    /// <summary>
    /// 缴纳主体社会统一信用代码
    /// </summary>
    public string? PaymentCode { get; set; }

    /// <summary>
    /// 状态(0：待审核 1：已退还 )
    ///</summary>
    public 保证金退还状态 Status { get; set; }

    /// <summary>
    /// 状态
    /// </summary>
    public string? StatusName { get; set; }

    /// <summary>
    /// 申请退还金额
    ///</summary>
    public decimal? ApplyAmount { get; set; }

    /// <summary>
    /// 申请理由
    ///</summary>
    public string? ApplyReason { get; set; }

    /// <summary>
    /// 退还决定单位
    ///</summary>
    public string? RefundOrganization { get; set; }

    /// <summary>
    /// 退还决定单位统一社会信用代码
    ///</summary>
    public string? RefundOrganizationCode { get; set; }

    /// <summary>
    /// 动用通知文书号
    ///</summary>
    public string? NotificationLetter { get; set; }

    /// <summary>
    /// 退还日期
    ///</summary>
    public DateTime? RefundTime { get; set; }

    /// <summary>
    /// 退还金额
    ///</summary>
    public decimal? RefundAmount { get; set; }
    public decimal? Balance { get; set; }

    /// <summary>
    /// 退还说明
    ///</summary>
    public string? RefundReason { get; set; }

    /// <summary>
    /// 创建时间
    ///</summary>
    public DateTime? CreateTime { get; set; }

    /******* 额外显示字段 ******/

    /// <summary>
    /// 项目名称
    /// </summary>
    public string? ProjectName { get; set; }

    /// <summary>
    /// 项目编号
    /// </summary>
    public string? ProjectNum { get; set; }

    /// <summary>
    /// 保证金编号
    /// </summary>
    public string? GuaranteeAccountCode { get; set; }

    /// <summary>
    /// 保证金方式
    /// </summary>
    public string? GuaranteeAccountTypeName { get; set; }

    /// <summary>
    /// 缴存类型
    /// </summary>
    public string? DepositeTypeName { get; set; }

    /// <summary>
    /// 缴存起始日期/有效期起始起始日期
    ///</summary>
    public DateTime? StartDate { get; set; }

    /// <summary>
    /// 应缴纳金额
    ///</summary>
    public decimal? Payable { get; set; }

    /// <summary>
    /// 已缴纳金额/担保金额/保单金额
    ///</summary>
    public decimal? Paid { get; set; }

    /// <summary>
    /// 差异化缴存金额（应缴纳金额和已缴纳金额的差值，取正值）
    ///</summary>
    public decimal? Difference { get; set; }
}

/// <summary>
/// 保证金退还扩展
/// </summary>
public static class ProjectGuaranteeRefundExceptions
{
    /// <summary>
    /// 返回前端显示数据
    /// </summary>
    /// <param name="entity"></param>
    /// <param name="applicationContext"></param>
    /// <returns></returns>
    public static ProjectGuaranteeRefundDto AsDto(this ProjectGuaranteeRefund entity, IApplicationContext applicationContext)
    {
        ProjectGuaranteeRefundDto projectGuaranteeRefundDto = new();
        var dto = applicationContext.GetService<IMapper>().Map(entity, projectGuaranteeRefundDto);
        var projectService = applicationContext.GetService<ProjectService>();
        var project = projectService.GetProjectInfo(entity.ProjectId);

        var projectGuaranteePaymentService = applicationContext.GetService<ProjectGuaranteePaymentService>();

        var projectGuaranteePayment = projectGuaranteePaymentService.GetProjectGuaranteePayment(entity.GuaranteePaymentId ?? 0);
        if (projectGuaranteePayment != null)
        {
            var projectGuaranteeAccountService = applicationContext.GetService<ProjectGuaranteeAccountService>();
            var projectGuaranteeAccount = projectGuaranteeAccountService.GetProjectGuaranteeAccount(projectGuaranteePayment.GuaranteeAccountId);
            dto.GuaranteeAccountCode = projectGuaranteeAccount?.Code ?? string.Empty;
            dto.GuaranteeAccountTypeName = projectGuaranteeAccount?.Type.GetDescription() ?? string.Empty;
            dto.DepositeTypeName = projectGuaranteePayment.DepositeType?.GetDescription() ?? string.Empty;
            dto.StartDate = projectGuaranteePayment.StartDate;
            dto.Payable = projectGuaranteePayment.Payable;
            dto.Paid = projectGuaranteePayment.Paid;
            dto.Difference = projectGuaranteePayment.Difference;
           dto.Balance= projectGuaranteePayment.Balance;
            if (entity.Id == 0)
            {
                dto.PaymentName = projectGuaranteePayment.PaymentName;
                dto.PaymentCode = projectGuaranteePayment.PaymentCode;
            }
        }
        if (project != null)
        {
            dto.ProjectName = project.Name;
            dto.ProjectNum = project.PrjNum;

            #region 主管单位数据权限

            if (applicationContext.CurrentUser is IDeptUser deptUser)
            {
                if (deptUser.IsSuperAdministrator())
                    dto.HasEdit = true;
                else if (deptUser.IsRSAdministrator())
                {
                    if (deptUser.Level == AdministrativeLevel.City && project.CityCode == deptUser.CityCode)
                        dto.HasEdit = true;
                    if (deptUser.Level == AdministrativeLevel.Area && project.AreaCode == deptUser.AreaCode)
                        dto.HasEdit = true;
                    if (deptUser.Level == AdministrativeLevel.Province)
                        dto.HasEdit = true;
                }
                else dto.HasEdit = false;
            }

            #endregion 主管单位数据权限
        }
        dto.StatusName = dto.Status.GetDescription();
        return dto;
    }
}